package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.comm.DataAndView;
import com.erp.comm.ResultCode;
import com.erp.pojo.Goods;
import com.erp.pojo.Provider;
import com.erp.service.GoodsService;
import com.erp.service.ProviderService;
import com.erp.vo.GoodsVo;
import com.erp.vo.ProviderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private  GoodsService goodsService;
    @Autowired
    private ProviderService providerService;
    @RequestMapping("loadAllGoods")
    public  DataAndView loadAllGoods(GoodsVo goodsVo){
        IPage<Goods> page = new Page<>(goodsVo.getPage(), goodsVo.getLimit());

        //条件查询
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        //判断是否为空
        if (goodsVo.getProviderid()!=null&&goodsVo.getProviderid()!=0){
            qw.like("providerid",goodsVo.getProviderid());
        }
        if (goodsVo.getGoodsname()!=null){
            qw.like("goodsname",goodsVo.getGoodsname());
        }
        if (goodsVo.getProductcode()!=null){
            qw.like("productcode",goodsVo.getProductcode());
        }
        if (goodsVo.getPromitcode()!=null){
            qw.like("promitcode",goodsVo.getPromitcode());
        }
        if (goodsVo.getDescription()!=null){
            qw.like("description",goodsVo.getDescription());
        }
        if (goodsVo.getSize()!=null){
            qw.like("size",goodsVo.getSize());
        }
        goodsService.page(page,qw);
        //return new DataAndView(page.getRecords());
        //查询结果
        List<Goods> list=page.getRecords();
        List<Goods> list2=new ArrayList<>();
        System.out.println(list);
        for (Goods good:list){
            Integer pid =good.getProviderid();
            Provider p =providerService.getById(pid);
            good.setProvidername(p.getProvidername());
            list2.add(good);
        }
        return new DataAndView(list2);
    }
    //删除
    @RequestMapping("deleteGoods")
    public ResultCode deleteGoods(Goods goods){
        try {
            boolean bool = goodsService.removeById(goods);

            if (bool) {
                return ResultCode.SUCCESS_DELETE;
            } else {
                return ResultCode.FAIL_DELETE;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_DELETE;
        }


    }
    //添加
    @RequestMapping("addGoods")
            public  ResultCode addGoods(Goods goods) {
        //获取用户填写的数据
//      //防止更行后 添加的时候id有值
        goods.setId(null);
        boolean bool = goodsService.save(goods);
        try {
            if (bool) {
                return ResultCode.SUCCESS_ADD;
            } else {
                return ResultCode.FAIL_ADD;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_ADD;
        }


    }
    //修改
    @RequestMapping("updateGoods")
    public ResultCode updateGoods(Goods goods) {

        try {
            boolean bool = goodsService.updateById(goods);
            if (bool) {
                return ResultCode.SUCCESS_UPDATE;
            } else {
                return ResultCode.FAIL_UPDATE;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_UPDATE;
        }

    }
    //查询所有商品
    @RequestMapping("loadAllGoodsForSelect")
    public DataAndView  loadAllGoodsForSelect(){
        //查询所有商品
        List<Goods> list = goodsService.list();
        List<Goods >  list2=new ArrayList<>();
        for (Goods good:list){
            //获取当前商品的供货商ID
            Integer provideId=good.getProviderid();
            //通过供货商id查对应的供货商
            Provider provider =providerService.getById(provideId);
            //设置当前的商品的供货商名称
            good.setProvidername(provider.getProvidername());
            list2.add(good);
        }
        return new DataAndView(list2);
    }
    @RequestMapping("loadGoodsByProviderId")
    public DataAndView loadGoodsByProviderId(Integer providerid){

        //通过ID查所有商品
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        if (providerid != 0){
            qw.eq("providerid",providerid);
        }
        List<Goods> lists = goodsService.list(qw);
        List<Goods> list2 = new ArrayList<>();
        for (Goods good:lists){
            //通过供应商查id对应的供应商
            Provider provider = providerService.getById(providerid);
            //设置当前的商品的供货名称
            good.setProvidername(provider.getProvidername());
            list2.add(good);
        }

        return new DataAndView(lists);
    }
}
